//
//  TrackVideo.m
//  CoolTennisBall
//
//  Created by CA on 16/3/28.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "TrackVideo.h"
#import "UIImage+YFExtension.h"
#import <SDWebImage/UIImageView+WebCache.h>


@implementation TrackVideo

- (void)toggleChecked
{
    [[RLMRealm defaultRealm] beginWriteTransaction];
    self.isSelected = !self.isSelected;
    [[RLMRealm defaultRealm] commitWriteTransaction];
}

- (NSString *)documentPath
{
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingString:self.path];
   
    return path;
}


- (void)removeVideo
{
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:self.documentPath]) {
        NSError *error;
        [manager removeItemAtPath:self.documentPath error:&error];
        
        if (!error) {
            NSLog(@"删除成功");
        }
    }
}

- (void)removeVideoImage {
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    
    if ([manager fileExistsAtPath:[documentPath stringByAppendingPathComponent:self.imagePath]]) {
        NSError *error;
        [manager removeItemAtPath:self.documentPath error:&error];
        if (!error) {
            NSLog(@"缩略图片删除成功");
        }
    }
}

+ (NSArray *)ignoredProperties {
    return @[@"videoImage"];
}

- (UIImage *)videoImage {
    if (_videoImage == nil) {
        _videoImage = [UIImage thumbnailImageForVideo:[NSURL fileURLWithPath:self.documentPath] atTime:0];
    }
    return _videoImage;
}

- (UIImage *)thumbnailImageForVideo
{
    if (self.videoImage == nil) {
        self.videoImage = [UIImage thumbnailImageForVideo:[NSURL fileURLWithPath:self.documentPath] atTime:0];
    }
    
    return self.videoImage;
}


@end

@implementation VideoTrackItem

- (instancetype)initWithSpeed:(NSInteger)speed
                     strength:(NSInteger)strength
                          arc:(NSInteger)arc
                  swingSecond:(NSInteger)swingSecond
{
    self = [super init];
    if (self) {
        _speed = speed;
        _strength = strength;
        _arc = arc;
        _swingSecond = swingSecond;
    }
    return self;
}

@end


